package org.zsk.cs.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.zsk.cs.dao.HotVideoDao;
import org.zsk.cs.dao.UserDao;
import org.zsk.cs.domain.po.HotVideo;
import org.zsk.cs.domain.po.User;
import org.zsk.cs.domain.vo.HotVideoVo;
import org.zsk.cs.exception.BizException;
import org.zsk.cs.service.UploadService;
import org.zsk.cs.util.UserUtils;
import org.zsk.fastdev.assign.Assign;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *     上传文件业务实现
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/4/14
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class UploadServiceImpl implements UploadService {

    /**
     * 头像路径
     */
    @Value("${app.path.avatar}")
    private String avatarPath;
    /**
     * 热点视频路径
     */
    @Value("${app.path.hotvideo}")
    private String hotVideoPath;
    /**
     * 资源获取前缀
     */
    @Value("${spring.mvc.static-path-pattern}")
    private String resourcePrefix;

    private final UserDao userDao;
    private final HotVideoDao hotVideoDao;
    private final Assign assign;

    @Autowired
    public UploadServiceImpl(UserDao userDao, HotVideoDao hotVideoDao, Assign assign) {
        this.userDao = userDao;
        this.hotVideoDao = hotVideoDao;
        this.assign = assign;
    }

    @Override
    public String uploadAvatar(MultipartFile avatar) {
        if (avatar.isEmpty()) {
            log.debug("文件不存在");
            throw new BizException("文件不存在");
        }
        String avatarName = avatar.getOriginalFilename();
        if (StringUtils.isEmpty(avatarName)){
            log.debug("文件名为空");
            throw new BizException("文件名为空");
        }
        String suffixName = avatarName.substring(avatarName.lastIndexOf("."));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newFileName = sdf.format(new Date()) + UUID.randomUUID().toString().replace("-","") + suffixName;
        File filePath = new File(avatarPath,newFileName);
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        try {
            User user=userDao.selectById(UserUtils.getCurrentUser().getId());
            user.setAvatar(getResourcePrefix()+newFileName);
            userDao.updateById(user);
            log.debug("头像信息存储成功");
            avatar.transferTo(new File(avatarPath+File.separator+ newFileName));
        } catch (IOException e) {
            log.debug("头像文件存储失败");
            throw new BizException("头像文件存储失败");
        }
        log.info("头像上传成功");
        return getResourcePrefix()+newFileName;
    }

    @Override
    public String uploadHotVideo(HotVideoVo hotVideoVo) throws Exception {
        if (hotVideoVo.getFile().isEmpty()) {
            log.debug("视频文件不存在");
            throw new BizException("视频文件不存在");
        }
        String fileName = hotVideoVo.getFile().getOriginalFilename();
        if (StringUtils.isEmpty(fileName)){
            log.debug("视频文件名为空");
            throw new BizException("视频文件名为空");
        }
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newFileName = sdf.format(new Date()) + UUID.randomUUID().toString().replace("-","") + suffixName;
        File filePath = new File(hotVideoPath,newFileName);
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        try {
            HotVideo hotVideo=new HotVideo();
            assign.assign(hotVideoVo,hotVideo);
            hotVideo.setPath(getResourcePrefix()+newFileName);
            hotVideoDao.insert(hotVideo);
            log.debug("视频信息存储成功");
            hotVideoVo.getFile().transferTo(new File(hotVideoPath+File.separator+ newFileName));
        } catch (IOException e) {
            log.debug("视频文件存储失败");
            throw new BizException("视频文件存储失败");
        }
        log.info("视频上传成功");
        return newFileName;
    }

    /**
     * @return 获取去掉后缀 ** 的静态资源前醉
     */
    private String getResourcePrefix(){
        return resourcePrefix.substring(0,resourcePrefix.length()-2);
    }
}
